package com.source.lightfowl.idcardgrantandrecovery.service.impl;

import com.source.lightfowl.VO.IdCardGrantVO;
import com.source.lightfowl.basedata.dao.IdCardDao;
import com.source.lightfowl.idcardgrantandrecovery.dao.IdCardGrantDao;
import com.source.lightfowl.idcardgrantandrecovery.entity.IdCardGrant;
import com.source.lightfowl.idcardgrantandrecovery.service.IdCardGrantService;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.dao.RoleDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.person.entity.Role;
import com.source.lightfowl.utils.StringTOArrayList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 14:54
 */
@Service
public class IdCardGrantServiceImpl implements IdCardGrantService {
    @Autowired
    private IdCardGrantDao idCardGrantDao;
    @Autowired
    private IdCardDao idCardDao;
    @Autowired
    private PersonDao personDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<IdCardGrantVO> findAll() {

        List<IdCardGrant> list = idCardGrantDao.findAll();
        List<IdCardGrantVO> collect = list.stream().map(s -> {
            IdCardGrantVO vo = new IdCardGrantVO();
            BeanUtils.copyProperties(s, vo);

            //查找发放人资料
//            Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getGrantPerson()).longValue()));
            Person user = personDao.findVO(s.getGrantPerson());
            //发放人姓名 角色名
            if (user != null) {
                vo.setDeliverName(user.getLoginName());

                //发放人角色名 （10.15版本的修改）
                String roleId = user.getRole();
                String rname = roleDao.getOne(roleId).getRname();
                vo.setDeliverRoteName(rname);
            }
            //查找发放人资料

            Person user1 = personDao.findVO(s.getReceivePerson());
            //发放人姓名 角色名
            if (user1 != null) {

                vo.setGetName(user1.getLoginName());
                vo.setGetRoteName(roleDao.getOne(user1.getRole()).getRname());
            }

            //创建者
            Person user2 = personDao.findVO(s.getCreateBy());
            if (user2 != null){
                vo.setCreateName(user.getName());
            }


            return vo;
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public Page<IdCardGrant> findAllPage(int page, int pageNum) {
        return idCardGrantDao.findAll(PageRequest.of(page - 1, pageNum));
    }

    @Override
    public IdCardGrantVO findById(int id) {
        Optional<IdCardGrant> optional = idCardGrantDao.findById(id);
        IdCardGrantVO vo = new IdCardGrantVO();
        if (optional.isPresent()) {
            IdCardGrant s = optional.get();

            BeanUtils.copyProperties(s, vo);

            Person user = personDao.findVO(s.getGrantPerson());
            //发放人姓名 角色名
            if (user != null) {
                vo.setDeliverName(user.getLoginName());
                //发放人角色名 （10.15版本的修改）
                String roleId = user.getRole();
                //String rname = roleDao.getOne(roleId).getRname();
                if (roleDao.findById(roleId).isPresent()) {
                    vo.setDeliverRoteName(roleDao.findById(roleId).get().getRname());
                }
            }

            Person user1 = personDao.findVO(s.getReceivePerson());
            //接收人姓名 角色名
            if (user1 != null) {

                vo.setGetName(user1.getLoginName());
                //vo.setGetRoteName(roleDao.getOne(user1.getRole()).getRname());
                Optional<Role> optional1 = roleDao.findById(user1.getRole());
                if (optional1.isPresent()) {
                    String rname = optional1.get().getRname();
                    vo.setGetRoteName(rname);
                }
            }

            return vo;
        }
        return new IdCardGrantVO();

    }

    @Override
    public void save(IdCardGrant sh) {
        String idCardNumber = String.valueOf(sh.getCardId());
        //设置发放时间
        if (sh.getGrantDate() == null) {
            sh.setGrantDate(LocalDateTime.now().toString());
        }
        //id卡创建时间 前端传递过来
        try {
            idCardGrantDao.save(sh);
        } catch (Exception e) {
            System.out.println("id卡发放异常");
            e.printStackTrace();
        }
        String receivePersonId = sh.getReceivePerson();
        //更新user表的记录
        personDao.updateIdCardById(idCardNumber, receivePersonId);
        //发放后修改idcard当前状态
        idCardDao.updateGrant(receivePersonId, idCardNumber);
        //发放后影响回收记录？目前没有影响
    }

    @Override
    public void update(IdCardGrant sh) {
        //设置发放时间
        if (sh.getGrantDate() == null) {
            sh.setGrantDate(LocalDateTime.now().toString());
        }
        //id卡创建时间 前端传递过来
        try {
            idCardGrantDao.save(sh);
        } catch (Exception e) {
            System.out.println("id卡发放异常");
            e.printStackTrace();
        }
    }

    @Override
    public void deleteById(int id) {
        idCardGrantDao.deleteById(id);
    }

    @Override
    public void deleteBatch(String ids) {
        ArrayList<Integer> list = StringTOArrayList.toArrayList(ids);
        idCardGrantDao.deleteBatch(list);
    }

    @Override
    public Long getCount() {
        Long count = idCardGrantDao.count();
        return count;
    }
}
